﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace School.Pages.Bus
{
    public partial class Pricing_stages : System.Web.UI.Page
    {
        TextBox txtvall;
        TextBox txtpall;
        TextBox txtvclass;
        TextBox txtpclass;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1207) == true)
                {

                    if (!IsPostBack)
                    {
                        ddlBind(ddCamp1, School.Service.Bus.GetAllCampuses().ToList());
                        ddlCertificate.Items.Insert(0, new ListItem("اختيار", "-1"));
                        if (ddCamp1.SelectedIndex > 0)
                            ddlBind(ddlCertificate, School.Service.Bus.GetCertificateByCamp(Convert.ToInt32(ddCamp1.SelectedValue.ToString())));
                    }
                    lbltestBus.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }

        protected void ddCamp1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCertificate.Items.Clear();
            ddlCertificate.Items.Insert(0, new ListItem("اختيار", "-1"));
            if (ddCamp1.SelectedIndex > 0)
                ddlBind(ddlCertificate, School.Service.Bus.GetCertificateByCamp(Convert.ToInt32(ddCamp1.SelectedValue.ToString())));
        }


        private void gvstageBind()
        {
            gvstage.DataSource = School.Service.Bus.GetStageByCerti(Convert.ToInt32(ddlCertificate.SelectedValue.ToString()));
            gvstage.DataBind();
        }

        protected void rdbvall_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((Control)sender).NamingContainer;
            txtgv(row, out txtvall, out txtpall, out txtvclass, out txtpclass);
            txtBibd(txtvall, true, txtpall, false, txtvclass, false, txtpclass, false);
            Getvalue(row.RowIndex);

        }
        private void txtBibd(TextBox txtvall, bool Etxtvall, TextBox txtpall, bool Etxtpall, TextBox txtvclass, bool Etxtvclass, TextBox txtpclass, bool Etxtpclass)
        {
            txtvall.Enabled = Etxtvall;
            txtpall.Enabled = Etxtpall;
            txtvclass.Enabled = Etxtvclass;
            txtpclass.Enabled = Etxtpclass;
        }
        private void txtgv(GridViewRow row, out TextBox txtvall, out TextBox txtpall, out TextBox txtvclass, out TextBox txtpclass)
        {
            txtvall = (TextBox)gvstage.Rows[row.RowIndex].FindControl("txtvall");
            txtpall = (TextBox)gvstage.Rows[row.RowIndex].FindControl("txtpall");
            txtvclass = (TextBox)gvstage.Rows[row.RowIndex].FindControl("txtvclass");
            txtpclass = (TextBox)gvstage.Rows[row.RowIndex].FindControl("txtpclass");
        }
        protected void rdvpall_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((Control)sender).NamingContainer;
            txtgv(row, out txtvall, out txtpall, out txtvclass, out txtpclass);
            txtBibd(txtvall, false, txtpall, true, txtvclass, false, txtpclass, false);
            Getvalue(row.RowIndex);
        }
        protected void rdbvclass_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((Control)sender).NamingContainer;
            txtgv(row, out txtvall, out txtpall, out txtvclass, out txtpclass);
            txtBibd(txtvall, false, txtpall, false, txtvclass, true, txtpclass, false);
            Getvalue(row.RowIndex);
        }
        protected void rdbpclass_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow row = (GridViewRow)((Control)sender).NamingContainer;
            txtgv(row, out txtvall, out txtpall, out txtvclass, out txtpclass);
            txtBibd(txtvall, false, txtpall, false, txtvclass, false, txtpclass, true);
            Getvalue(row.RowIndex);
        }

        protected void gvstage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int Id = Convert.ToInt32(gvstage.DataKeys[e.Row.RowIndex].Values["Id"].ToString());
                TextBox txtvall = (TextBox)e.Row.FindControl("txtvall");
                TextBox txtpall = (TextBox)e.Row.FindControl("txtpall");
                TextBox txtvclass = (TextBox)e.Row.FindControl("txtvclass");
                TextBox txtpclass = (TextBox)e.Row.FindControl("txtpclass");

                var q = School.Service.Bus.GetPrice_Increase(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), Id);
                if (q.Count() != 0)
                {
                    txtvall.Text = q.First().Increase_Value.ToString();
                    txtpall.Text = q.First().Increase_percent.ToString();
                    txtvclass.Text = q.First().Grade_Increase_Value.ToString();
                    txtpclass.Text = q.First().Grade_Increase_Percent.ToString();
                }
            }

        }

        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvstageBind();
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            bool re = false;
            foreach (GridViewRow Row in gvstage.Rows)
            {
                int Id = Convert.ToInt32(gvstage.DataKeys[Row.RowIndex].Values["Id"].ToString());
                txtgv(Row, out txtvall, out txtpall, out txtvclass, out txtpclass);
                if (txtvall.Enabled == true)
                {

                    //HFtxtvall,HFtxtpall,Hftxtvclass,HFtxtpclass
                    if (!string.IsNullOrEmpty(HFtxtvall.Value) || !string.IsNullOrEmpty(HFtxtpall.Value) || !string.IsNullOrEmpty(Hftxtvclass.Value) || !string.IsNullOrEmpty(HFtxtpclass.Value))
                    {
                        re = School.Service.Bus.UpdatePrice_Increase(Id, Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), Convert.ToDecimal(txtvall.Text), 0, 0, 0);
                    }
                    else
                        re = School.Service.Bus.AddPrice_Increase(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), Id, Convert.ToDecimal(txtvall.Text), 0, 0, 0);
                }
                else if (txtpall.Enabled == true)
                {
                    if (!string.IsNullOrEmpty(HFtxtvall.Value) || !string.IsNullOrEmpty(HFtxtpall.Value) || !string.IsNullOrEmpty(Hftxtvclass.Value) || !string.IsNullOrEmpty(HFtxtpclass.Value))
                    {
                        re = School.Service.Bus.UpdatePrice_Increase(Id, Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), 0, float.Parse(txtpall.Text), 0, 0);
                    }
                    else
                        re = School.Service.Bus.AddPrice_Increase(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), Id, 0, float.Parse(txtpall.Text), 0, 0);
                }
                else if (txtvclass.Enabled == true)
                {
                    if (!string.IsNullOrEmpty(HFtxtvall.Value) || !string.IsNullOrEmpty(HFtxtpall.Value) || !string.IsNullOrEmpty(Hftxtvclass.Value) || !string.IsNullOrEmpty(HFtxtpclass.Value))
                    {
                        re = School.Service.Bus.UpdatePrice_Increase(Id, Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), 0, 0, Convert.ToDecimal(txtvclass.Text), 0);
                    }
                    else
                        re = School.Service.Bus.AddPrice_Increase(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), Id, 0, 0, Convert.ToDecimal(txtvclass.Text), 0);
                }
                else if (txtpclass.Enabled == true)
                {
                    if (!string.IsNullOrEmpty(HFtxtvall.Value) || !string.IsNullOrEmpty(HFtxtpall.Value) || !string.IsNullOrEmpty(Hftxtvclass.Value) || !string.IsNullOrEmpty(HFtxtpclass.Value))
                    {
                        re = School.Service.Bus.UpdatePrice_Increase(Id, Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), 0, 0, 0, float.Parse(txtpclass.Text));
                    }
                    else
                        re = School.Service.Bus.AddPrice_Increase(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), Convert.ToInt32(ddlCertificate.SelectedValue.ToString()), Id, 0, 0, 0, float.Parse(txtpclass.Text));
                }
            }
            if (re == true)
            {
                lbltestBus.Text = "تم التحديث بنجاح";
                gvstageBind();
            }
            else
            {
                lbltestBus.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltestBus.Visible = true;
        }
        private void Getvalue(int rowindex)
        {
            foreach (GridViewRow row in gvstage.Rows)
            {
                if (row.RowIndex == rowindex)
                {
                    TextBox txtvall = (TextBox)row.FindControl("txtvall");
                    if (txtvall.Text.Length > 0)
                        HFtxtvall.Value = txtvall.Text;
                    TextBox txtpall = (TextBox)row.FindControl("txtpall");
                    if (txtpall.Text.Length > 0)
                        HFtxtpall.Value = txtpall.Text;
                    TextBox txtvclass = (TextBox)row.FindControl("txtvclass");
                    if (txtvclass.Text.Length > 0)
                        Hftxtvclass.Value = txtvclass.Text;
                    TextBox txtpclass = (TextBox)row.FindControl("txtpclass");
                    if (txtpclass.Text.Length > 0)
                        HFtxtpclass.Value = txtpclass.Text;
                }
            }
        }
    }
}